//
//  SignStoreViewController.m
//  WuShiCity
//
//  Created by chengjie on 16/7/6.
//  Copyright © 2016年 chengjie. All rights reserved.
//

#import "SignStoreViewController.h"
#import "BaseCollectionViewCell.h"
#import "SignStoreScrollView.h"
#import "ScrollCollectionReusableView.h"

@interface SignStoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation SignStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - --------CollectionView的代理方法----------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 3) {
        
        return 30;
    }else {
        
        return 4;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseCollectionViewCell *baseCell = nil;
    if (indexPath.section == 0) {
        
        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalNeedCell" forIndexPath:indexPath];
        return baseCell;
    }if (indexPath.section == 1) {
        
        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SpecialCell" forIndexPath:indexPath];
        baseCell.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
        return baseCell;
//    } if (indexPath.section == 2) {
//
//        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PrizeCell" forIndexPath:indexPath];
//        return baseCell;
    }if (indexPath.section == 3) {
        
        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PrizeCell" forIndexPath:indexPath];
        baseCell.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
        return baseCell;
    }else {
        return baseCell;
    }
   
        
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    NSMutableArray *mArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 5; i++) {
        
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d%d.jpg",i,i]];
        [mArray addObject:image];
    }
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section == 1) {
            
            [headerView addSubview:[self setSectionHeaderViewWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height) titleName:@"特别推荐" btnTitle:@"更多奖品"]];
        }if (indexPath.section == 2) {
            
            [headerView addSubview:[self setSectionHeaderViewWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height) titleName:@"精彩活动" btnTitle:@"更多游戏"]];
        }if (indexPath.section == 3) {
            
            [headerView addSubview:[self setSectionHeaderViewWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height) titleName:@"奖品优选" btnTitle:@""]];
        }
        return headerView;
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        
        ScrollCollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            
            [footerView initWithFrame:footerView.frame imagesArray:mArray period:2];
        }else {
            
            footerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
        }
        return footerView;
    }
    return nil;
}



//item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return CGSizeMake((kScreenWidth)/4, (kScreenWidth)/4);
    }else if (indexPath.section == 1) {
        
        return CGSizeMake((kScreenWidth-1)/2, kScreenWidth/4);
    } else if (indexPath.section == 3) {
        
        return CGSizeMake(kScreenWidth/2, kScreenWidth/2);
    }
    else {
        
        return CGSizeMake(0,0);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else if(section == 1) {
        
        return UIEdgeInsetsMake(0, 0, 0, 0);
    } else {
        
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return CGSizeMake(0,0);
    }else {
        
        return CGSizeMake(kScreenWidth, 50);
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return CGSizeMake(kScreenWidth, 100);
    }else {
        
        return CGSizeMake(0, 10);
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}



#pragma mark - ----------设置分区头视图---------
- (UIView *)setSectionHeaderViewWithFrame:(CGRect)frame titleName:(NSString *)title btnTitle:(NSString *)btnTitle {
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 5, view.frame.size.height-20)];
    line.backgroundColor = [UIColor greenColor];
    [view addSubview:line];
    UILabel *titleName = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, view.frame.size.height)];
    titleName.text = title;
    [view addSubview:titleName];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setFrame:CGRectMake(view.frame.size.width-100, 0, 90, view.frame.size.height)];
    [btn setTitle:btnTitle forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(moreBtn:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    UILabel *moreLabel = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width-20, 0, 10, view.frame.size.height-5)];
    moreLabel.text = @">";
    [view addSubview:moreLabel];
    return view;
}

- (void)moreBtn:(UIButton *)sender {
    
    
}

#pragma mark - ---------laying----------
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.minimumLineSpacing = 10;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalNeedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalNeedCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"SpecialCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SpecialCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PrizeGoodCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PrizeCell"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        
        [_collectionView registerClass:[ScrollCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        _collectionView.contentMode = UICollectionViewScrollDirectionHorizontal;
        _collectionView.contentMode = UICollectionViewScrollDirectionVertical;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
